package edu.usm.cs.roberge;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class CourseScheduler {
	static int studentCnt = 0;
	static int courseCnt = 0;
	
	Map<Integer, Course> courses = new HashMap<Integer, Course>(); 
	Map<Integer, Student> students = new HashMap<Integer, Student>();
	
	@WebMethod
	public Integer registerStudent(Student student) {
		student.setId(studentCnt++);
		students.put(student.getId(), student);
		return student.getId();
	}
	
	@WebMethod
	public Integer registerCourse(Course course) {
		course.setId(courseCnt++);
		courses.put(course.getId(), course);
		return course.getId();
	}
	
	@WebMethod
	public Boolean enrollStudentInCourse(Integer studentId, Integer courseId) {
		Student student = students.get(studentId);
		Course course = courses.get(courseId);
		
		if (student == null || course == null) {
			return false;
		}
		
		if (student.getSchedule().hasCourse(course)) {
			return false;
		}
		student.getSchedule().enroll(course);
		
		return true;
	}
	
	@WebMethod
	public Student findStudent(Integer studentId) {
		return students.get(studentId);
	}
}
